package com.wyhello.tube4.spring.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wyhello.tube4.pojo.User;
import com.wyhello.tube4.spring.model.UserOperateModel;

@Controller
public class UserOperateController {
	@Autowired
	private UserOperateModel userOperateModel;
	
	@RequestMapping("/user/saveUser")
	public String saveUser(User user){
		user.setPower(0);
		userOperateModel.saveUser(user);
		return "success";
	}
	
	@RequestMapping("/user/queryUser")
	public ModelAndView queryUser(){
		List<User> users = userOperateModel.queryUser();
		ModelAndView model = new ModelAndView("list","users",users);
		return model;
	}
	@RequestMapping("/user/deleteUser")
	public String deleteUser(HttpServletRequest request){
		String deletid = request.getParameter("deletid");
		userOperateModel.deleteUser("("+deletid+")");
		return "success";
	}
	@RequestMapping("/user/updateUser")
	public String updateUser(User user){
		User u = userOperateModel.getUserById(user.getId());
		u.setUserName(user.getUserName());
		u.setPassword(user.getPassword());
		u.setPower(user.getPower());
		userOperateModel.updateUser(u);
		return "success";
	}
	@RequestMapping("/user/queryUpdate")
	public ModelAndView queryUserById(int id){
		User user = userOperateModel.getUserById(id);
		ModelAndView model = new ModelAndView("update","users",user);
		return model;
	}
	
}
